import { reactive } from 'vue'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function useState<T extends Record<string, any>>(initialState: T) {
  const initial = { ...initialState }
  const state = reactive<T>(initialState)

  const setState = (payload: Partial<T>) => {
    for (const key in payload) {
      if (Object.prototype.hasOwnProperty.call(payload, key)) {
        const newValue = payload[key]
        if (state[key] !== newValue) {
          // eslint-disable-next-line @typescript-eslint/no-explicit-any
          ;(state as any)[key] = newValue
        }
      }
    }
  }
  const resetState = () => {
    Object.assign(state, initial)
  }
  return {
    state,
    setState,
    resetState,
  }
}
